package com.parking.order.consumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parking.common.core.model.RedisKey;
import com.parking.common.core.model.StateData;
import com.parking.common.web.util.RedisUtil;
import com.parking.order.client.dto.CertifiedInfoDto;
import com.parking.order.dao.EntranceInfoDao;
import com.parking.order.dao.OrderInfoDao;
import com.parking.order.dao.OrderOvertimeDao;
import com.parking.order.model.EntranceInfo;
import com.parking.order.model.OrderInfo;
import com.parking.order.model.OrderOvertime;
import com.parking.order.util.BasicInfoUtil;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.time.Duration;

/**
 * 超时订单消费者类
 * */
@Component
public class OvertimeConsumer {
    /**
     * 超时订单数据层
     * */
    @Autowired
    private OrderOvertimeDao orderOvertimeDao;

    /**
     * 进出场信息数据层
     * */
    @Autowired
    private EntranceInfoDao entranceInfoDao;

    /**
     * 其他模块调用
     * */
    @Autowired
    private BasicInfoUtil basicInfoUtil;

    /**
     * Redis工具类
     * */
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 订单信息数据层
     * */
    @Autowired
    private OrderInfoDao orderInfoDao;

    /**
     * 生成超时订单
     * 参数 entranceInfoId 进出场信息
     * */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(value="orderOvertime")})
    public void orderOvertimeConsumer(String entranceInfoId) throws Exception {
        System.out.println("==================新增超时订单=========================");
        EntranceInfo entranceInfo = entranceInfoDao.selectById(entranceInfoId);
        OrderInfo order = orderInfoDao.selectOne(new QueryWrapper<OrderInfo>().eq("order_info_num",entranceInfo.getOrderInfoNum()));

        System.out.println("订单编号:"+order.getOrderInfoNum());

        OrderOvertime overtime = new OrderOvertime(order.getOrderInfoId(),order.getOrderInfoNum(),order.getStallInfoId(),order.getEndTime(),entranceInfo.getEntranceOutTime(),StateData.ORDER_OVERTIME_NOT_PAY,order.getUserInfoId());
        overtime.setCertifiedInfoId(order.getCertifiedInfoId());

        long seconds = Duration.between(overtime.getStartTime(), overtime.getEndTime()).getSeconds();
        int enduHour = Math.toIntExact(seconds % (60 * 60) == 0 ? seconds / (60 * 60) : (seconds / (60 * 60)) + 1);
        overtime.setEnduHour(enduHour);

        CertifiedInfoDto certified = basicInfoUtil.getCertifiedInfo(order.getCertifiedInfoId());
        BigDecimal unitPrice = null;
        if(order.getStallType().equals("普通车位")) unitPrice = certified.getStallUnitPrice();
        if(order.getStallType().equals("充电车位")) unitPrice = certified.getChargeUnitPrice();
        assert unitPrice != null;
        overtime.setPayPrice(unitPrice.multiply(BigDecimal.valueOf(enduHour)));
        overtime.setUpdateTime(overtime.getEndTime());
        orderOvertimeDao.insert(overtime);

        if(redisUtil.exist(RedisKey.certifiedOrderOvertime(order.getCertifiedInfoId()))){
            int badCommentNum = Integer.parseInt(redisUtil.get(RedisKey.certifiedOrderOvertime(order.getCertifiedInfoId())));
            redisUtil.set(RedisKey.certifiedOrderOvertime(order.getCertifiedInfoId()), String.valueOf(badCommentNum+1));
        }else{
            redisUtil.set(RedisKey.certifiedOrderOvertime(order.getCertifiedInfoId()), "1");
        }

        System.out.println("===============超时订单生成完成=====================");
    }

}
